package com.gmlive.common.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.util.TypedValue;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.appcompat.widget.AppCompatButton;

import com.gmlive.common.ui.R;
import com.gmlive.common.ui.util.ResHelper;

/**
 * @Author: 姜志敏
 * @CreateDate: 2019-07-12 10:03
 */
public class RoundButton extends AppCompatButton {
    private static final int BG_STYLE_NORMAL = 0;
    private static final int BG_STYLE_SOLID = 1;
    private static final int BG_STYLE_STROKE = 2;

    private int mBackgroundStyle = BG_STYLE_NORMAL;
    private int mCornerRadius;

    public RoundButton(Context context) {
        this(context, null);
    }

    public RoundButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.style.IkUiButtonStyle);
    }

    public RoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(new ContextThemeWrapper(context, resolveButtonTheme(context, 0)), attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundButton);
        mBackgroundStyle = a.getInt(R.styleable.RoundButton_backgroundStyle, BG_STYLE_NORMAL);
        a.recycle();
    }

    private static int resolveButtonTheme(@NonNull Context context, @StyleRes int resid) {
        // Check to see if this resourceId has a valid package ID.
        if (((resid >>> 24) & 0x000000ff) >= 0x00000001) {   // start of real resource IDs.
            return resid;
        } else {
            TypedValue outValue = new TypedValue();
            context.getTheme().resolveAttribute(R.attr.buttonStyle, outValue, true);
            return outValue.resourceId;
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mCornerRadius = getMeasuredHeight() / 2;

        applyStyle();
    }

    private void applyStyle() {
        switch (mBackgroundStyle) {
            case BG_STYLE_NORMAL:
                setNormalStyle();
                break;
            case BG_STYLE_SOLID:
                setSolidStyle();
                break;
            case BG_STYLE_STROKE:
                setStokeStyle();
                break;
        }
    }

    private void setNormalStyle() {
        StateListDrawable drawable = ResHelper.createRoundButtonBackground(getContext(), mCornerRadius);
        setBackground(drawable);

        int colorNormal = getResources().getColor(R.color.ik_ui_button_normal_text);
        int colorDisable = getResources().getColor(R.color.ik_ui_button_disable_text);
        setTextColor(ResHelper.createTextStateColor(colorNormal, colorNormal, colorDisable));
    }

    private void setSolidStyle() {
        Context context = getContext();
        int themeColor = ResHelper.getThemeColor(context);
        StateListDrawable drawable = ResHelper.createRoundButtonBackground(context, themeColor, mCornerRadius, true);
        setBackground(drawable);

        int colorNormal = Color.WHITE;
        int colorDisable = getResources().getColor(R.color.ik_ui_button_disable_text);
        setTextColor(ResHelper.createTextStateColor(colorNormal, colorNormal, colorDisable));
    }

    private void setStokeStyle() {
        Context context = getContext();
        int themeColor = ResHelper.getThemeColor(context);
        StateListDrawable drawable = ResHelper.createRoundButtonBackground(context, themeColor, mCornerRadius, false);
        setBackground(drawable);

        int colorNormal = themeColor;
        int colorDisable = getResources().getColor(R.color.ik_ui_button_disable_text);
        setTextColor(ResHelper.createTextStateColor(colorNormal, Color.WHITE, colorDisable));
    }
}
